package com.jadcon.bedbreakfast.domain;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Component;

@Component("suiteRepository")
public class SuiteRepositoryImpl implements SuiteRepository {

	private SimpleJdbcTemplate simpleJdbcTemplate;

	@SuppressWarnings("unchecked")
	public List<Suite> getSuiteList() {
		String sql = "SELECT S_id, name, number, beds, cost, description, imageName FROM suite ORDER BY number";
		return this.simpleJdbcTemplate.query(sql, new SuiteMapper());
	}

	@SuppressWarnings("unchecked")
	public Suite getSuiteByNumber(int number) {
		String sql = "SELECT name, number, beds, cost, description, imageName, S_id FROM suite where number=?";
		List<Suite> suiteList = simpleJdbcTemplate.query(sql,
				new SuiteMapper(), number);
		Suite suite = suiteList.get(0);
		return suite;
	}
	
	@SuppressWarnings("unchecked")
	public int getCapacity(int suiteNumber){
		String sql = "select name, number, beds, cost, description, imageName from suite where number=?";
		List<Suite> suiteList = simpleJdbcTemplate.query(sql, new SuiteMapper(), suiteNumber);
		Suite suite = suiteList.get(0);
		return suite.getBeds();

	}

	@SuppressWarnings("unchecked")
	private static final class SuiteMapper implements RowMapper {

		public Suite mapRow(ResultSet rs, int rowNum) throws SQLException {
			String name = rs.getString("name");
			int number = rs.getInt("number");
			int beds = rs.getInt("beds");
			int cost = rs.getInt("cost");
			String description = rs.getString("description");
			String imageName = rs.getString("imageName");
			Suite suite = new Suite(name, number, beds, description, imageName,
					cost);
			return suite;

		}

	}

	@Autowired
	public void setDataSource(DataSource dataSource) {
		simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}

}